//
//  BiXingTianKongTiViewController.swift
//  CKC
//

//var oneBtn:UIButton?
//var twoBtn:UIButton?
//var threeBtn:UIButton?
//var fourBtn:UIButton?
//var fiveBtn:UIButton?
//var sixBtn:UIButton?
//var sevenBtn:UIButton?
//var eightBtn:UIButton?
//var nineBtn:UIButton?
//var dianBtn:UIButton?
//var lingBtn:UIButton?
//var nextBtn:UIButton?

//  Created by 张振东 on 2016/10/20.
//  Copyright © 2016年 ZZD. All rights reserved.
//

import UIKit
import RealmSwift

class BiXingTianKongTiViewController: UIViewController{

    let screenWidth = UIScreen.main.bounds.width
    let screenHeight = UIScreen.main.bounds.height
    var navigationHeight:CGFloat?
    
    //定义一个iamgeView,存储随机生成的图片
    var topImageView:UIImageView?
    
    //定义一个变量:记录题号的值,默认为1
    var tiHao:Int = 1
    
    //定义几个label对象,保存题干内容
    var tiHaoLabel:UILabel?
    var tiGanNeiRongLabel:UILabel?
    var hengXianLabel:UILabel?
    var userInputNumberLabel:UILabel?
    
    //定义一个textView对象,该对象做键盘
    var jianPanView:UIView?
    
    
    //创建第一层的三个label对象
    var firstFloorLeftImagView:UIImageView?
    var firstFloorMiddleImageView:UIImageView?
    var firstFloorRightImageView:UIImageView?
    
    //创建20个按钮
    var ziCiBtn:UIButton?
    var sevenBtn:UIButton?
    var eightBtn:UIButton?
    var nineBtn:UIButton?
    var fuHaoBtn:UIButton?
    var wenHaoBtn:UIButton?
    var fourBtn:UIButton?
    var fiveBtn:UIButton?
    var sixBtn:UIButton?
    var douHaoJuHaoBtn:UIButton?
    var ziMuBtn:UIButton?
    var oneBtn:UIButton?
    var twoBtn:UIButton?
    var threeBtn:UIButton?
    var shanChuBtn:UIButton?
    var numberBtn:UIButton?
    var qiuBtn:UIButton?
    var lingBtn:UIButton?
    
    //定义两个按钮:保存,下一题
    var konggeBtn:UIButton?
    var huanHangBtn:UIButton?
    
    
    
    //定义两个按钮:保存,下一题
    var saveBtn1:UIButton?
    var nextQuestionBtn1:UIButton?
    
    
    //定义一个变量,用于存储从文件夹中读取的图片资源列表
    var imageList:[String]?
    var topImageName:String?
    //定义一个变量,保存生成的图片的笔形吗
    var topImageViewBiXingCode:Int = 0
    
    //定义三个image
    var firstFloorLeftImage:UIImage?
    var firstFloorMiddleImage:UIImage?
    var firstFloorRightImage:UIImage?
    
    override func viewDidLoad() {
        
        fengZhuangViewDidLoad()
        //弹出一个提示框,显示没有收藏题了
        let alertController = UIAlertController(title: "温馨提示",
                                                message: "亲,您共有20道热身题哦,如果您输入的值与笔形对应,那么字体颜色为绿色,代表正确,否则输入字体为灰色!!", preferredStyle: .alert)
        let okAction = UIAlertAction(title: "好的", style: .default, handler: {
            action in
            print("点击了确定")
        })
        alertController.addAction(okAction)
        self.present(alertController, animated: true, completion: nil)

        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
    }
    
    /*
     作者:张振东
     日期:2017-10-20
     功能:封装lable属性
     */
    func fengZhuangLabelAttributes(_ label:UILabel){
        label.font = UIFont(name: "FontAwesome",size: 15)
        switch label {
        case tiHaoLabel!:
            label.text = "\(tiHao)"
            label.font = UIFont(name: "FontAwesome",size: 20)
            break
        case tiGanNeiRongLabel!:
            label.text = "、以上笔形对应的笔形码为"
            break
        case hengXianLabel!:
            label.text = "_______."
            break
        case userInputNumberLabel!:
            label.text = ""
            label.textAlignment = .center
            label.font = UIFont(name: "FontAwesome",size: 18)
            break
        default:
            print("封装完毕")
        }
    }
    
    
    /*
     作者:张振东
     日期:2017-10-20
     功能:封装button属性
     */
    func fengZhuangButtonAttributes(_ button:UIButton){
        
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        button.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
        button.layer.cornerRadius = 20
        switch button {
        case saveBtn1!:
            button.setTitle("收藏", for: UIControlState())
            break
        case nextQuestionBtn1!:
            button.setTitle("下一个", for: UIControlState())
            break
            
        default:
            print("封装button")
        }
    }

    
    /*
     作者:张振东
     时间:2016-10-22
     功能:实现图片的随记生成
     */
    func fengZhuangViewDidLoad(){
        fengZhuangKongJianShuXing()
        //给0、1、2、3、4、5、6、7、8、9、下一个添加点击事件
        oneBtn?.addTarget(self, action: #selector(clickOne), for: .touchUpInside)
        twoBtn?.addTarget(self, action: #selector(clickTwo), for: .touchUpInside)
        threeBtn?.addTarget(self, action: #selector(clickThree), for: .touchUpInside)
        fourBtn?.addTarget(self, action: #selector(clickFour), for: .touchUpInside)
        fiveBtn?.addTarget(self, action: #selector(clickFive), for: .touchUpInside)
        sixBtn?.addTarget(self, action: #selector(clickSix), for: .touchUpInside)
        sevenBtn?.addTarget(self, action: #selector(clickSeven), for: .touchUpInside)
        eightBtn?.addTarget(self, action: #selector(clickEight), for: .touchUpInside)
        nineBtn?.addTarget(self, action: #selector(clickNine), for: .touchUpInside)
        lingBtn?.addTarget(self, action: #selector(clickZero), for: .touchUpInside)
        
        saveBtn1?.addTarget(self, action: #selector(clickSaveBtn), for: .touchUpInside)
        nextQuestionBtn1?.addTarget(self, action: #selector(clickNextQuestion), for: .touchUpInside)
        
        let biXingPath = Bundle.main.path(forResource: "bixingimage",ofType: nil, inDirectory: nil)
        print(biXingPath as Any)
        
        //从制定的目录下面读取文件
        do{
            try imageList = FileManager.default.contentsOfDirectory(atPath: biXingPath!)
            if imageList != nil{
                imageList?.remove(at: 0)
            }
        }
        catch let nsError as NSError{
            print(nsError)
        }
        
        self.view.addSubview(topImageView!)
        self.view.addSubview(tiHaoLabel!)
        self.view.addSubview(tiGanNeiRongLabel!)
        self.view.addSubview(hengXianLabel!)
        self.view.addSubview(userInputNumberLabel!)
        jianPanView?.addSubview(firstFloorLeftImagView!)
        jianPanView?.addSubview(firstFloorMiddleImageView!)
        jianPanView?.addSubview(firstFloorRightImageView!)
        jianPanView?.addSubview(ziCiBtn!)
        jianPanView?.addSubview(sevenBtn!)
        jianPanView?.addSubview(eightBtn!)
        jianPanView?.addSubview(nineBtn!)
        jianPanView?.addSubview(fuHaoBtn!)
        jianPanView?.addSubview(wenHaoBtn!)
        jianPanView?.addSubview(fourBtn!)
        jianPanView?.addSubview(fiveBtn!)
        jianPanView?.addSubview(sixBtn!)
        jianPanView?.addSubview(douHaoJuHaoBtn!)
        jianPanView?.addSubview(ziMuBtn!)
        jianPanView?.addSubview(oneBtn!)
        jianPanView?.addSubview(twoBtn!)
        jianPanView?.addSubview(threeBtn!)
        jianPanView?.addSubview(shanChuBtn!)
        jianPanView?.addSubview(numberBtn!)
        jianPanView?.addSubview(qiuBtn!)
        jianPanView?.addSubview(lingBtn!)
        jianPanView?.addSubview(konggeBtn!)
        jianPanView?.addSubview(huanHangBtn!)
        self.view.addSubview(jianPanView!)
        self.view.addSubview(saveBtn1!)
        self.view.addSubview(nextQuestionBtn1!)
        shengChengTuPianBingFuZhi()
    }

    
    
    func fengZhuangKongJianShuXing(){
        self.title = "填空题热身"
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white,NSFontAttributeName:UIFont(name: "STHeitiSC-Medium", size: 20.0)!]
        
        self.view.backgroundColor = UIColor(red: 246,green: 246,blue: 246,alpha: 1)
        let leftBarButton = UIButton(frame:CGRect(x: 0,y: 0,width: 20,height: 20))
        leftBarButton.setBackgroundImage(UIImage(named: "fanhui"), for: UIControlState())
        leftBarButton.addTarget(self, action: #selector(backToPrevious), for: .touchUpInside)
        let leftBarButtonItem = UIBarButtonItem(customView: leftBarButton)
        self.navigationItem.leftBarButtonItems = [leftBarButtonItem]
        
        //设置右边的按钮
        let rightBarButton = UIButton(frame:CGRect(x: screenWidth-20,y: 0,width: 20,height: 20))
        rightBarButton.setBackgroundImage(UIImage(named: "chakanshoucang"), for: UIControlState())
        let rightBarButtonItem = UIBarButtonItem(customView: rightBarButton)
        rightBarButton.addTarget(self, action: #selector(chaKanShouCangTianKongTiMu), for: .touchUpInside)
        self.navigationItem.rightBarButtonItems = [rightBarButtonItem]
        
        //获取导航栏的高度
        navigationHeight = self.navigationController?.navigationBar.bounds.size.height
        
        //初始化iamgeView
        topImageView = UIImageView(frame:CGRect(x:50,y:navigationHeight!+15,width:screenWidth-100,height:screenWidth-100))
        topImageView!.contentMode = .scaleAspectFit
        
        //初始化题干label对象
        tiHaoLabel = UILabel(frame:CGRect(x: 10,y: navigationHeight!+screenWidth-90,width: 25,height: 30))
        fengZhuangLabelAttributes(tiHaoLabel!)
        
        //初始化题干内容label对象
        tiGanNeiRongLabel = UILabel(frame:CGRect(x: 30,y: navigationHeight!+screenWidth-90,width: 210,height: 30))
        fengZhuangLabelAttributes(tiGanNeiRongLabel!)
        
        hengXianLabel = UILabel(frame:CGRect(x: 240,y: navigationHeight!+screenWidth-90,width: 60,height: 30))
        fengZhuangLabelAttributes(hengXianLabel!)
        
        userInputNumberLabel = UILabel(frame:CGRect(x: 240,y: navigationHeight!+screenWidth-90,width: 60,height: 30))
        fengZhuangLabelAttributes(userInputNumberLabel!)
        
        //初始化键盘UIview
        jianPanView = UIView(frame:CGRect(x: 0, y: screenHeight-265, width: screenWidth, height: 220))
        jianPanView?.backgroundColor = UIColor(red: 209/255,green: 213/255,blue: 219/255,alpha: 1)
        jianPanView?.isUserInteractionEnabled = true
    
        
        //初始化第一层的三个label对象
        firstFloorLeftImagView = UIImageView(frame:CGRect(x: 0,y: 0,width: 60,height: 40))
        firstFloorLeftImage = UIImage(named: "firstFloorLeftImage.gif")
        firstFloorLeftImagView?.image = firstFloorLeftImage
        
        
        firstFloorMiddleImageView = UIImageView(frame:CGRect(x: 60,y: 0,width: screenWidth-80,height: 40))
        firstFloorMiddleImage = UIImage(named: "firstFloorMiddleImage.gif")
        firstFloorMiddleImageView?.image = firstFloorMiddleImage
        
        
        firstFloorRightImageView = UIImageView(frame:CGRect(x: screenWidth-40,y: 0,width: 40,height: 40))
        firstFloorRightImage = UIImage(named: "firstFloorRightImage.gif")
        firstFloorRightImageView?.image = firstFloorRightImage
        
        
        //初始化20个按钮
        ziCiBtn = UIButton(frame:CGRect(x: 0,y: 40+5,width: 50,height: 40))
        ziCiBtn?.setTitle("字/词", for: UIControlState())
        ziCiBtn?.setImage(UIImage(named: "ziCiImage.gif"), for: UIControlState())
        
        sevenBtn = UIButton(frame:CGRect(x: 50+5,y: 40+5,width: (screenWidth-120)/3,height: 40))
        sevenBtn?.layer.cornerRadius = 10
        sevenBtn?.backgroundColor = UIColor.white
        sevenBtn?.setTitle("7", for: UIControlState())
        sevenBtn?.isUserInteractionEnabled = true
        sevenBtn?.setTitleColor(UIColor(red: 0,green: 0,blue: 0,alpha: 1), for: UIControlState())
        
        eightBtn = UIButton(frame:CGRect(x: 50+5*2+(screenWidth-120)/3,y: 40+5,width: (screenWidth-120)/3,height: 40))
        eightBtn?.setTitleColor(UIColor(red: 0,green: 0,blue: 0,alpha: 1), for: UIControlState())
        eightBtn?.backgroundColor = UIColor.white
        eightBtn?.layer.cornerRadius = 10
        eightBtn?.setTitle("8", for: UIControlState())
        eightBtn?.isUserInteractionEnabled = true
        
        nineBtn = UIButton(frame:CGRect(x: 50+5*3+2*(screenWidth-120)/3,y: 40+5,width: (screenWidth-120)/3,height: 40))
        nineBtn?.titleLabel?.textColor = UIColor(red: 0,green: 0,blue: 0,alpha: 1)
        nineBtn?.backgroundColor = UIColor.white
        nineBtn?.setTitleColor(UIColor(red: 0,green: 0,blue: 0,alpha: 1), for: UIControlState())

        nineBtn?.layer.cornerRadius = 10
        nineBtn?.setTitle("9", for: UIControlState())
        nineBtn?.isUserInteractionEnabled = true
        
        fuHaoBtn = UIButton(frame:CGRect(x: screenWidth-50,y: 40+5,width: 50,height: 40))
        fuHaoBtn?.setTitle("符", for: UIControlState())
        fuHaoBtn?.setImage(UIImage(named: "fuHaoImage"), for: UIControlState())
        
        wenHaoBtn = UIButton(frame:CGRect(x: 0,y: 40+5+40+5,width: 50,height: 40))
        wenHaoBtn?.setTitle("?", for: UIControlState())
        wenHaoBtn?.setImage(UIImage(named: "wenHaoImage.gif"), for: UIControlState())
        
        
        fourBtn = UIButton(frame:CGRect(x: 50+5,y: 40+5+40+5,width: (screenWidth-120)/3,height: 40))
        fourBtn?.setTitleColor(UIColor(red: 0,green: 0,blue: 0,alpha: 1), for: UIControlState())
        fourBtn?.backgroundColor = UIColor.white
        fourBtn?.layer.cornerRadius = 10
        fourBtn?.setTitle("4", for: UIControlState())
        fourBtn?.isUserInteractionEnabled = true
        
        
        fiveBtn = UIButton(frame:CGRect(x: 50+5*2+(screenWidth-120)/3,y: 40+5+40+5,width: (screenWidth-120)/3,height: 40))
        fiveBtn?.setTitleColor(UIColor(red: 0,green: 0,blue: 0,alpha: 1), for: UIControlState())
        fiveBtn?.backgroundColor = UIColor.white
        fiveBtn?.layer.cornerRadius = 10
        fiveBtn?.setTitle("5", for: UIControlState())
        fiveBtn?.isUserInteractionEnabled = true
        
        sixBtn = UIButton(frame:CGRect(x: 50+5*3+2*(screenWidth-120)/3,y: 40+5+40+5,width: (screenWidth-120)/3,height: 40))
        sixBtn?.setTitleColor(UIColor(red: 0,green: 0,blue: 0,alpha: 1), for: UIControlState())
        sixBtn?.backgroundColor = UIColor.white
        sixBtn?.layer.cornerRadius = 10
        sixBtn?.setTitle("6", for: UIControlState())
        sixBtn?.isUserInteractionEnabled = true
        
        douHaoJuHaoBtn = UIButton(frame:CGRect(x: screenWidth-50,y: 40+5+40+5,width: 50,height: 40))
        douHaoJuHaoBtn?.setTitle("，。", for: UIControlState())
        douHaoJuHaoBtn?.setImage(UIImage(named: "douHaoJuHaoImage.gif"), for: UIControlState())
        
        ziMuBtn = UIButton(frame:CGRect(x: 0,y: 40+5+40+5+40+5,width: 50,height: 40))
        ziMuBtn?.setTitle("ABC", for: UIControlState())
        ziMuBtn?.setImage(UIImage(named: "ziMuImage.gif"), for: UIControlState())
        
        oneBtn = UIButton(frame:CGRect(x: 50+5,y: 40+5+40+5+40+5,width: (screenWidth-120)/3,height: 40))
        oneBtn?.setTitleColor(UIColor(red: 0,green: 0,blue: 0,alpha: 1), for: UIControlState())
        oneBtn?.backgroundColor = UIColor.white
        oneBtn?.layer.cornerRadius = 10
        oneBtn?.setTitle("1", for: UIControlState())
        oneBtn?.isUserInteractionEnabled = true
        
        twoBtn = UIButton(frame:CGRect(x: 50+5*2+(screenWidth-120)/3,y: 40+5+40+5+40+5,width: (screenWidth-120)/3,height: 40))
        twoBtn?.setTitleColor(UIColor(red: 0,green: 0,blue: 0,alpha: 1), for: UIControlState())
        twoBtn?.backgroundColor = UIColor.white
        twoBtn?.layer.cornerRadius = 10
        twoBtn?.setTitle("2", for: UIControlState())
        twoBtn?.isUserInteractionEnabled = true
        
        threeBtn = UIButton(frame:CGRect(x: 50+5*3+2*(screenWidth-120)/3,y: 40+5+40+5+40+5,width: (screenWidth-120)/3,height: 40))
        threeBtn?.setTitleColor(UIColor(red: 0,green: 0,blue: 0,alpha: 1), for: UIControlState())
        threeBtn?.backgroundColor = UIColor.white
        threeBtn?.layer.cornerRadius = 10
        threeBtn?.setTitle("3", for: UIControlState())
        threeBtn?.isUserInteractionEnabled = true
        
        shanChuBtn = UIButton(frame:CGRect(x: screenWidth-50,y: 40+5+40+5+40+5,width: 50,height: 40))
        shanChuBtn?.setTitle("<", for: UIControlState())
        shanChuBtn?.setImage(UIImage(named: "qingChuImage.gif"), for: UIControlState())
        
        numberBtn = UIButton(frame:CGRect(x: 0,y: 40+5+40+5+40+5+40+5,width: 50,height: 40))
        numberBtn?.setTitle("123", for: UIControlState())
        numberBtn?.setImage(UIImage(named: "numberImage.gif"), for: UIControlState())
        
        qiuBtn = UIButton(frame:CGRect(x: 50+5,y: 40+5+40+5+40+5+40+5,width: 50,height: 40))
        qiuBtn?.setImage(UIImage(named: "qiuImage.gif"), for: UIControlState())
        
        lingBtn = UIButton(frame:CGRect(x: 50+5+50+5,y: 40+5+40+5+40+5+40+5,width: (screenWidth-120)/3,height: 40))
        lingBtn?.setTitleColor(UIColor(red: 0,green: 0,blue: 0,alpha: 1), for: UIControlState())
        lingBtn?.backgroundColor = UIColor.white
        lingBtn?.layer.cornerRadius = 10
        lingBtn?.setTitle("0", for: UIControlState())
        lingBtn?.isUserInteractionEnabled = true
        
        konggeBtn = UIButton(frame:CGRect(x: 50+5+50+5+5+(screenWidth-120)/3,y: 40+5+40+5+40+5+40+5,width: (screenWidth-120)/3,height: 40))
        konggeBtn?.setTitleColor(UIColor(red: 0,green: 0,blue: 0,alpha: 1), for: UIControlState())
        konggeBtn?.backgroundColor = UIColor.white
        konggeBtn?.layer.cornerRadius = 10
        konggeBtn?.setTitle("空格", for: UIControlState())
        konggeBtn?.isUserInteractionEnabled = false
        
        huanHangBtn = UIButton(frame:CGRect(x: 50+5+50+5+5+5+2*(screenWidth-120)/3,y: 40+5+40+5+40+5+40+5,width: (screenWidth-120)/3,height: 40))
        huanHangBtn?.backgroundColor = UIColor(red: 170/255,green: 170/255,blue: 170/255,alpha: 1)
        huanHangBtn?.setTitleColor(UIColor(red: 0,green: 0,blue: 0,alpha: 1), for: UIControlState())
        huanHangBtn?.layer.cornerRadius = 10
        huanHangBtn?.setTitle("换行", for: UIControlState())
        huanHangBtn?.isUserInteractionEnabled = false
        
        //初始化保存按钮
        saveBtn1 = UIButton(frame:CGRect(x: 10,y: screenHeight-43,width: (screenWidth-40)/2,height: 40))
        fengZhuangButtonAttributes(saveBtn1!)
        
        nextQuestionBtn1 = UIButton(frame:CGRect(x: screenWidth/2+10,y: screenHeight-43,width: (screenWidth-40)/2,height: 40))
        
        //初始化下一题按钮
        fengZhuangButtonAttributes(nextQuestionBtn1!)

    }
    /*
     作者:张振东
     时间:2016-10-22
     功能:实现图片的随记生成
     */
    func shengChengTuPianBingFuZhi(){
        if let cancelImageListOptional = imageList{
            print(cancelImageListOptional)
            //获得该[String]数组的长度
            let arrayOfLength = cancelImageListOptional.count
            print(arrayOfLength)
            //随记生成一个0-[String]数组的长度的整数
            let randomNumber:UInt32 = arc4random_uniform(UInt32(arrayOfLength))
            print(randomNumber)
            //通过第三步得到的数字可以获得该数组对应的值(也就是图片的名字)
            topImageName = cancelImageListOptional[Int(randomNumber)]
            topImageView?.image = UIImage(named: topImageName!)
            
            topImageViewBiXingCode = Int(((topImageName! as NSString)).substring(with: NSMakeRange(0, 1)))!
            print(topImageViewBiXingCode)
            
        }
    }
    
    /*
     作者:张振东
     日期:2016-10-22
     功能:点击按钮"1"
     */
    func clickOne(){
        self.userInputNumberLabel?.text = "1"
        if topImageViewBiXingCode == 1{
            self.userInputNumberLabel?.textColor = UIColor.green
        }else{
            self.userInputNumberLabel?.textColor = UIColor.gray
        }
    }
    
    /*
     作者:张振东
     日期:2016-10-22
     功能:点击按钮"2"
     */
    func clickTwo(){
        self.userInputNumberLabel?.text = "2"
        if topImageViewBiXingCode == 2{
            self.userInputNumberLabel?.textColor = UIColor.green
        }else{
            self.userInputNumberLabel?.textColor = UIColor.gray
        }

    }
    
    /*
     作者:张振东
     日期:2016-10-22
     功能:点击按钮"3"
     */
    func clickThree(){
        self.userInputNumberLabel?.text = "3"
        if topImageViewBiXingCode == 3{
            self.userInputNumberLabel?.textColor = UIColor.green
        }else{
            self.userInputNumberLabel?.textColor = UIColor.gray
        }

    }
    
    /*
     作者:张振东
     日期:2016-10-22
     功能:点击按钮"4"
     */
    func clickFour(){
        self.userInputNumberLabel?.text = "4"
        if topImageViewBiXingCode == 4{
            self.userInputNumberLabel?.textColor = UIColor.green
        }else{
            self.userInputNumberLabel?.textColor = UIColor.gray
        }

    }
    
    /*
     作者:张振东
     日期:2016-10-22
     功能:点击按钮"5"
     */
    func clickFive(){
        self.userInputNumberLabel?.text = "5"
        if topImageViewBiXingCode == 5{
            self.userInputNumberLabel?.textColor = UIColor.green
        }else{
            self.userInputNumberLabel?.textColor = UIColor.gray
        }

    }
    
    /*
     作者:张振东
     日期:2016-10-22
     功能:点击按钮"6"
     */
    func clickSix(){
        self.userInputNumberLabel?.text = "6"
        if topImageViewBiXingCode == 6{
            self.userInputNumberLabel?.textColor = UIColor.green
        }else{
            self.userInputNumberLabel?.textColor = UIColor.gray
        }

    }
    
    /*
     作者:张振东
     日期:2016-10-22
     功能:点击按钮"7"
     */
    func clickSeven(){
        self.userInputNumberLabel?.text = "7"
        if topImageViewBiXingCode == 7{
            self.userInputNumberLabel?.textColor = UIColor.green
        }else{
            self.userInputNumberLabel?.textColor = UIColor.gray
        }

    }
    
    /*
     作者:张振东
     日期:2016-10-22
     功能:点击按钮"8"
     */
    func clickEight(){
        self.userInputNumberLabel?.text = "8"
        if topImageViewBiXingCode == 8{
            self.userInputNumberLabel?.textColor = UIColor.green
        }else{
            self.userInputNumberLabel?.textColor = UIColor.gray
        }

    }

    /*
     作者:张振东
     日期:2016-10-22
     功能:点击按钮"9"
     */
    func clickNine(){
        self.userInputNumberLabel?.text = "9"
        if topImageViewBiXingCode == 9{
            self.userInputNumberLabel?.textColor = UIColor.green
        }else{
            self.userInputNumberLabel?.textColor = UIColor.gray
        }

    }
    
    /*
     作者:张振东
     日期:2016-10-22
     功能:点击按钮"0"
     */
    func clickZero(){
        self.userInputNumberLabel?.text = "0"
        if topImageViewBiXingCode == 0{
            self.userInputNumberLabel?.textColor = UIColor.green
        }else{
            self.userInputNumberLabel?.textColor = UIColor.gray
        }

    }
    
    /*
     作者:张振东
     日期:2016-10-23
     功能:点击按钮"收藏"
        点击收藏需要处理以下逻辑:
        1、将需要传递的属性封装为对象
        2、使用realm持久化数据到默认数据库
     */
    func clickSaveBtn(){
        //将需要保存的属性封装为类对象进行存储
        let biXingTianKongTi = BiXingTianKongTi()
        
        biXingTianKongTi.topImageName = self.topImageName
        print(biXingTianKongTi.topImageName as Any)
        
        biXingTianKongTi.tiHao = self.tiHaoLabel?.text
        print(biXingTianKongTi.tiHao as Any)
        
        biXingTianKongTi.userInputNumber = self.userInputNumberLabel?.text
            print(biXingTianKongTi.userInputNumber as Any)
        
        //使用默认的数据库
        let realm = try! Realm()
        try! realm.write {
            realm.add(biXingTianKongTi)
        }
        
        //打印出数据库地址
        print(realm.configuration.fileURL as Any)
        
        //弹出一个提示框,显示没有收藏题了
        let alertController = UIAlertController(title: "温馨提示",
                                                message: "亲,收藏成功了!!", preferredStyle: .alert)
        let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        let okAction = UIAlertAction(title: "好的", style: .default, handler: {
            action in
            print("点击了确定")
        })
        alertController.addAction(cancelAction)
        alertController.addAction(okAction)
        self.present(alertController, animated: true, completion: nil)
    }
    
    /*
     作者:张振东
     日期:2016-10-23
     功能:点击按钮"下一个"
     */
    func clickNextQuestion(){
        //1、题号会自动加一
        tiHao = tiHao + 1
        
        if tiHao <= 20{
            self.tiHaoLabel?.text = "\(tiHao)"
            
            //2、将上次的对象清空
            self.tiHaoLabel?.text = ""
            self.tiGanNeiRongLabel?.text = ""
            self.userInputNumberLabel?.text = ""
            self.hengXianLabel?.text = ""
            
            //3、题干内容重新加载,不发生变化
            fengZhuangViewDidLoad()

        }else{
            //让下一个按钮失去交互性
            nextQuestionBtn1?.isUserInteractionEnabled = false
            //热身结束了
            let alertController = UIAlertController(title: "温馨提示",
                                                    message: "亲,热身结束了!!", preferredStyle: .alert)
            let okAction = UIAlertAction(title: "好的", style: .default, handler: {
                action in
                print("点击了确定")
            })
            alertController.addAction(okAction)
            self.present(alertController, animated: true, completion: nil)
        }
        
        
    }
    
    func chaKanShouCangTianKongTiMu(){
        let chaKanBiXingShouCangTianKongTiViewController = ChaKanBiXingShouCangTianKongTiViewController()
        self.navigationController?.pushViewController(chaKanBiXingShouCangTianKongTiViewController, animated: true)
    }
    
    func backToPrevious(){
       self.navigationController?.popViewController(animated: true)
    }
    
    override func resignFirstResponder() -> Bool {
        return true
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
